<?php
/**
 * Description:
 * Author: hezhizheng
 * Date: 2018/6/11
 * Time: 10:34
 * Created by Created by Panxsoft.
 */

namespace App\Models\OpenWechat;

use App\Models\Customer\Asset;
use App\Models\Panx\Product;
use App\Models\Store\Pack;
use App\Models\Store\Store;
use App\Observers\OSSCleanObserver;
use App\Services\Utils\Bosable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

/**
 * Class WechatUserFavor
 * @package App\Models\OpenWechat
 * @property array product_ids
 * @property int wechat_user_id
 * @property int id
 * @property Store store
 * @property-write Product[]|\Illuminate\Database\Eloquent\Collection products
 * @mixin Builder
 */
class WechatUserFavor extends Model
{
    use Bosable;

    protected $key_bos_key_prefix = 'wechat_user/try_on_photo/';

    protected $table = 'wechat_user_favors';

    protected $guarded = [];

    protected $casts = [
        'product_ids'    => 'array',
        'wechat_user_id' => 'int'
    ];

    public $bos_columns = ['try_on_photo'];

    /**
     * @param $try_on_photo
     * @throws \Exception
     */
    public function setTryOnPhotoAttribute($try_on_photo)
    {
        if (is_file($try_on_photo)) {
            $this->attributes['try_on_photo'] = $this->uploadPostFile($try_on_photo, $this->key_bos_key_prefix);
        }
    }

    public function getTryOnPhotoAttribute()
    {
        if (!$this->attributes['try_on_photo']) return null;

        return $this->getStaticUrl($this->attributes['try_on_photo']);
    }

    public function pack()
    {
        return $this->belongsTo(Pack::class, 'pack_id', 'id');
    }

    public function store()
    {
        return $this->belongsTo(Store::class, 'store_id', 'id');
    }

    public function asset()
    {
        return $this->belongsTo(Asset::class, 'asset_id', 'id');
    }

    public function getProductsForPackAsset()
    {
        $asset_ids = DB::table('pack_pivot_asset')->where('pack_id', $this->pack_id)->pluck('asset_id');

        $pack_product_ids = DB::table('pack_asset_pivot_product')->whereIn('asset_id', $asset_ids)->pluck('product_id')->all();

        $asset_product_ids = DB::table('products')->where('asset_id', $this->asset_id)->pluck('id')->all();

        $product_ids = array_merge($pack_product_ids, $asset_product_ids);

        return Product::whereIn('id', $product_ids)->get();
    }

    /**
     * @return Collection|Product[]
     */
    public function getProductsForProductIds(): Collection
    {
        $product_ids = (array) $this->product_ids;

        return (new Product)
            ->whereIn('id', $product_ids)
            ->with('thumbImage')
            ->get();
    }

    /**
     * 是否绑定了微信用户（已被收藏）
     * @return bool
     */
    public function isWechatUserBound(): bool
    {
        return (bool) $this->wechat_user_id;
    }

    /**
     * 注册删除 bosKey 事件绑定
     */
    protected static function boot()
    {
        parent::boot();

        /*
         * 监听bos清理事件
         */
        self::observe(OSSCleanObserver::class);
    }
}